﻿using SYJ.IBLL;
using SYJ.BLL;
using SYJ.Common.Filters;
using SYJ.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SYJ.Model;
using SYJ.Common;
using SYJ.Model.ViewModel.Common;
using System.Web.Security;
using SYJ.Filter;

namespace SYJ.Logic.Controllers.admin
{
    public partial class LoginController : BaseController
    {
        ISysUserBLL bll = new SysUserBLL();
        ISysPermissionBLL PermissionBLL = new SysPermissionBLL();

        [HttpGet]
        [SkipAuthorize]
        public ActionResult Index()
        {
            // QueryModel qmodel = new QueryModel();
            // qmodel.PageSize = 3;
            // qmodel.PageIndex = 2;
            // List<SysPermission> list = PermissionBLL.GetPage(qmodel);
            // ViewBag.List = list;
            // foreach (SysPermission user in list)
            //{
            //    string aa = user.display_name;
            //}
            //Logger.Info(sb);
            return View();

        }
        
        [HttpPost]
        [SkipAuthorize]
        public ActionResult Index(LoginModel model)
        {


            if (string.IsNullOrEmpty(model.username) || string.IsNullOrEmpty(model.password) || string.IsNullOrEmpty(model.vercode))
            {
                ViewBag.Error = "用户名,密码,验证码等必填项不能为空!";
                return View();
            }
            if (!ValidateCode(model.vercode))
            {
                ViewBag.Error = "验证码错误,请重试!";
                return View();
            }
            if (Session["Session_Login_Count"] != null)
            {
                int loginCount = int.Parse(Session["Session_Login_Count"].ToString());
                if (loginCount > 3)
                {
                    ViewBag.Error = "您的账号已经锁定,请30分钟后重试!";
                    return View();
                }
                if (loginCount > 0 && loginCount <= 3)
                {
                    ViewBag.Error = "您的还有" + (3 - loginCount) + "机会尝试登陆,如忘记密码,可联系系统管理员!";
                    return View();
                }
            }


            SysUser user = bll.Login(model);
            if (user.id > 0)
            {
                List<SysPermission> plist = PermissionBLL.Get(string.Format(@"SELECT b.operate_id,s.id, s.parent_id, s.`name`, s.`explain`, s.`status`, s.`plat_from`, s.`seq`, s.`url`, s.`area_name`, s.`controller_name`, s.`action_name`, s.`display_name`, s.`iconcls`, s.`level`, s.`isMenu` from sys_permission_role_operate_bind b LEFT JOIN sys_permission s on b.permission_id=s.id WHERE  b.plat_from=0 and b.`status`=0 and  (b.type=0 and b.role_id in (SELECT role_id FROM sys_user_role_bind WHERE user_id={0})) 
or (b.type=1 and b.role_id ={0})",user.id));
                FormsAuthentication.SetAuthCookie(model.username, false);
                AppContext.Current.SetUser(user, plist);
                return Redirect("/admin/Default/Index");
            }
            else
            {
                if (Session["Session_Login_Count"] != null)
                {
                    int loginCount = int.Parse(Session["Session_Login_Count"].ToString());
                    if (loginCount > 0 && loginCount <= 3)
                    {
                        ViewBag.Error = "您的还有" + (3 - loginCount) + "机会尝试登陆,如忘记密码,可联系系统管理员!";
                        Session["Session_Login_Count"] = loginCount + 1;//标示错误
                        return View();
                    }
                }
                else
                {
                    Session["Session_Login_Count"] = 1;//标示错误
                }

                ViewBag.Error = "用户名或者密码错误登录失败,请重试!";
            }
            return View();

        }
        [SkipAuthorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return Redirect(FormsAuthentication.LoginUrl);
        }
        [SkipAuthorize]
        public ActionResult Error()
        {
            return View();

        }

        #region 验证码
        /// <summary>
        /// 对验证码输入的校验
        /// </summary>
        /// <param name="strValidateCode"></param>
        /// <returns></returns>
        bool ValidateCode(string strValidateCode)
        {
            if (Session["ValidateCode"] == null)
                return false;
            string validateCode = Session["ValidateCode"].ToString();
            if (strValidateCode.ToLower().Equals(validateCode.ToLower()))
            {
                return true;
            }
            return false;
        }
        [SkipAuthorize]
        public ActionResult ValidateCode()
        {
            new ValidateCode(System.Web.HttpContext.Current, "ValidateCode");
            return View();
        }
        #endregion


    }
}
